﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Web.Administration;
using CMS6Tools.Common.Utility;
using CommonLogging = Common.Logging;

namespace CMS6Tools.Common.IdentityImpersonate
{
    public static class IISUtility
    {
        #region Fields
        static readonly CommonLogging.ILog Logger = CommonLogging.LogManager.GetLogger(typeof(IISUtility));
        #endregion

        #region Internal Methods
        static Application GetApplication(this ServerManager serverManager, string applicationAliasName)
        {
            foreach (var site in serverManager.Sites)
            {
                foreach (var application in site.Applications)
                {
                    if (StringUtility.Equal(application.Path.TrimStart('/'), applicationAliasName))
                        return application;
                }
            }
            return null;
        }

        static void RestartAppPool(this ServerManager serverManager, string appPoolName)
        {
            var appPool = serverManager.ApplicationPools[appPoolName];
            if (appPool != null)
            {
                appPool.Stop();
                Thread.Sleep(10000);
                appPool.Start();
            }
        }

        static void StartAppPool(this ServerManager serverManager, string appPoolName)
        {
            var appPool = serverManager.ApplicationPools[appPoolName];
            if (appPool != null)
            {
                appPool.Start();
            }
        }

        static void RecycleAppPool(this ServerManager serverManager, string appPoolName)
        {
            var appPool = serverManager.ApplicationPools[appPoolName];
            if (appPool != null)
            {
                appPool.Recycle();
            }
        }

        static void StopAppPool(this ServerManager serverManager, string appPoolName, int? sleepTimeInSecond = 10000)
        {
            var appPool = serverManager.ApplicationPools[appPoolName];
            if (appPool != null)
            {
                appPool.Stop();
                if (sleepTimeInSecond.HasValue)
                    Thread.Sleep(sleepTimeInSecond.Value);
            }
        }
        #endregion

        #region Restart, Stop, Recycle

        #region Application
        public static void RestartAppPoolAccordingApplication(string applicationAliasName)
        {
            using (var serverManager = new ServerManager())
            {
                try
                {
                    var application = serverManager.GetApplication(applicationAliasName);
                    if (application != null)
                    {
                        serverManager.RestartAppPool(application.ApplicationPoolName);
                    }
                }
                catch (Exception e)
                {
                    Logger.Error("Restart Site Failed.", e);
                }
            }
        }
        #endregion

        #region Application Pool
        public static void StartAppPool(string appPoolName)
        {
            using (var serverManager = new ServerManager())
            {
                try
                {
                    serverManager.StartAppPool(appPoolName);
                }
                catch (Exception e)
                {
                    Logger.Error("Start Site Failed.", e);
                }
            }
        }

        public static void RecycleAppPool(string appPoolName)
        {
            using (var serverManager = new ServerManager())
            {
                try
                {
                    serverManager.RecycleAppPool(appPoolName);
                }
                catch (Exception e)
                {
                    Logger.Error("Recycle Site Failed.", e);
                }
            }
        }

        public static void StopAppPool(string appPoolName, int? sleepTimeInSecond = 10000)
        {
            using (var serverManager = new ServerManager())
            {
                try
                {
                    serverManager.StopAppPool(appPoolName, sleepTimeInSecond);
                }
                catch (Exception e)
                {
                    Logger.Error("Stop Site Failed.", e);
                }
            }
        }

        public static void RestartAppPool(string appPoolName)
        {
            using (var serverManager = new ServerManager())
            {
                try
                {
                    serverManager.RestartAppPool(appPoolName);
                }
                catch (Exception e)
                {
                    Logger.Error("Restart Site Failed.", e);
                }
            }
        }
        #endregion

        #endregion
    }
}
